import { getChannelToken, setChannelToken } from 'service/auth';
import { checkToken, login, loginnew,register, doctorInfo, apply, lowerChannel, lowerChannel_n, users, getChannelInfo, updatePassword, listChannelOrder } from 'api/channel';

import { getCreditInfo, getUsersSalesInfo, getFirstTeamInfo, getSecondTeamInfo, getMemberList, getChannelDetail, getListChannelRedeem, updateBankInfo } from 'api/channel';


const channel = {
    state: {
        channelToken: getChannelToken(),
    },

    mutations: {
        SET_CHANNEL_TOKEN: (state, token) => {
            state.channelToken = token;
        }
    },
    actions: {

        // 登录
        ChannelLogin({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                login(data).then(response => {
                    commit('SET_CHANNEL_TOKEN', true);
                    setChannelToken(true);
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //未注册的手机号会先自动注册，然后登录

        channelLoginNew({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                loginnew(data).then(response => {
                    commit('SET_CHANNEL_TOKEN', true);
                    setChannelToken(true);
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },

        ChannelRegister({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                register(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        GetDoctorInfo({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                doctorInfo(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        ChannelApply({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                apply(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        LowerChannel({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                lowerChannel(data).then(response => {
                    resolve(response.data);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        LowerChannel_n({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                lowerChannel_n(data).then(response => {
                    resolve(response.data);
                }).catch(error => {
                    reject(error);
                });
            });
        },

        ListChannelOrder({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                listChannelOrder(data).then(response => {
                    resolve(response.data);
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // LowerChannel({ commit, state }, data) {
        //   return new Promise((resolve, reject) => {
        //     lowerChannel(data).then(response => {
        //       const getDetail = async function() {
        //         let list = [];
        //         for (let [i, v] of response.data.entries()) {
        //           list[i] = v
        //           let detail = await getChannelInfo({
        //             channelId: v.id
        //           });
        //           list[i].channel = detail.data
        //         }
        //         return list;
        //       };
        //       resolve(getDetail());
        //     }).catch(error => {
        //       reject(error);
        //     });
        //   });
        // },
        Users({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                users(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        getChannelInfo({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                getChannelInfo(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        updatePassword({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                updatePassword(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        updateBankInfo({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                updateBankInfo(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        /**
        新版渠道需求 
        **/

        GetChannelDetail({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                getChannelDetail(data).then(response => {
                    resolve(response.data);
                }).catch(error => {
                    reject(error);
                });
            });
        },


        GetNewChannelInfo({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                let info = {};
                const getMoreInfo = async function() {
                    let credit = await getCreditInfo();
                    let user = await getUsersSalesInfo();
                    let level1 = await getFirstTeamInfo();
                    let level2 = await getSecondTeamInfo();
                    info = {
                        credit: credit.data,
                        user: user.data,
                        level1: level1.data,
                        level2: level2.data
                    }
                    return info;
                };
                resolve(getMoreInfo());
            });
        },
        GetNewMemberInfo({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                let info = {};
                const getMoreInfo = async function() {
                    let credit = await getCreditInfo();
                    let mermberList = await getMemberList();
                    info = {
                        credit: credit.data,
                        mermberList: mermberList.data,
                    }
                    return info;
                };
                resolve(getMoreInfo());
            });

        },
        getListChannelRedeem({ commit, state }, data) {
            return new Promise((resolve, reject) => {
                getListChannelRedeem(data).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },




    }
}

export default channel